package edu.sdjzu.exam.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import edu.sdjzu.exam.common.CommonResponse;
import edu.sdjzu.exam.dto.GradingDTO;
import edu.sdjzu.exam.service.exam.GradingService;
import edu.sdjzu.exam.service.exam.AutoGradingService.AutoGradingResult;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/grading")
@RequiredArgsConstructor
@SaCheckLogin
@SaCheckRole("teacher")
public class GradingController {

    private final GradingService gradingService;

    /**
     * 获取需要批改的考试列表
     */
    @GetMapping("/exams")
    public CommonResponse<List<GradingDTO.ExamGradingListDTO>> getExamsNeedingGrading() {
        try {
            List<GradingDTO.ExamGradingListDTO> exams = gradingService.getExamsNeedingGrading();
            return CommonResponse.success(exams);
        } catch (Exception e) {
            return CommonResponse.error("获取失败：" + e.getMessage());
        }
    }

    /**
     * 获取指定考试的考生批改列表
     */
    @GetMapping("/exams/{examId}/students")
    public CommonResponse<List<GradingDTO.StudentGradingListDTO>> getStudentsForGrading(@PathVariable Long examId) {
        try {
            List<GradingDTO.StudentGradingListDTO> students = gradingService.getStudentsForGrading(examId);
            return CommonResponse.success(students);
        } catch (Exception e) {
            return CommonResponse.error("获取失败：" + e.getMessage());
        }
    }

    /**
     * 获取学生的所有主观题答案
     */
    @GetMapping("/sessions/{sessionId}/subjective-answers")
    public CommonResponse<List<GradingDTO.SubjectiveAnswerDTO>> getSubjectiveAnswers(
            @PathVariable Long sessionId) {
        try {
            List<GradingDTO.SubjectiveAnswerDTO> answers = gradingService.getSubjectiveAnswersBySession(sessionId);
            return CommonResponse.success(answers);
        } catch (Exception e) {
            return CommonResponse.error("获取失败：" + e.getMessage());
        }
    }

    /**
     * 批改单个答案
     */
    @PostMapping("/grade-single")
    public CommonResponse<String> gradeSingleAnswer(@Valid @RequestBody GradingDTO gradingDTO) {
        try {
            gradingService.gradeSingleAnswer(gradingDTO);
            return CommonResponse.success("批改成功");
        } catch (Exception e) {
            return CommonResponse.error("批改失败：" + e.getMessage());
        }
    }

    /**
     * 批量批改学生的主观题
     */
    @PostMapping("/batch-grade")
    public CommonResponse<String> batchGradeStudent(@Valid @RequestBody GradingDTO.BatchGradingDTO batchGradingDTO) {
        try {
            gradingService.batchGradeStudent(batchGradingDTO);
            return CommonResponse.success("批量批改成功");
        } catch (Exception e) {
            return CommonResponse.error("批量批改失败：" + e.getMessage());
        }
    }

    /**
     * 完成学生批改（计算总分并更新状态）
     */
    @PostMapping("/complete/{sessionId}")
    public CommonResponse<GradingService.GradingResult> completeStudentGrading(
            @PathVariable Long sessionId) {
        try {
            GradingService.GradingResult result = gradingService.completeStudentGradingBySession(sessionId);
            return CommonResponse.success("批改完成", result);
        } catch (Exception e) {
            return CommonResponse.error("完成批改失败：" + e.getMessage());
        }
    }
    
    /**
     * 触发自动批卷
     */
    @PostMapping("/auto-grade/{sessionId}")
    public CommonResponse<AutoGradingResult> triggerAutoGrading(
            @PathVariable Long sessionId) {
        try {
            AutoGradingResult result = gradingService.triggerAutoGradingBySession(sessionId);
            return CommonResponse.success("自动批卷完成", result);
        } catch (Exception e) {
            return CommonResponse.error("自动批卷失败：" + e.getMessage());
        }
    }
} 